﻿using HCMS.CommonDtos;
using HCMS.Dtos.OAuth2;
using HCMS.Entities.OAuth2;
using HCMS.Extensions;
using HCMS.IRepositories.OAuth2;
using HCMS.IServices.OAuth2;
using HCMS.Repositories.OAuth2;
using System.Collections.Generic;
using System.Threading.Tasks;
using Volo.Abp.Domain.Repositories;

namespace HCMS.Services.OAuth2
{
    /// <summary>
    /// OAuth 2.0 应用 应用服务
    /// </summary>
    public class OAuth2AppAppService : CommonAppService<OAuth2DbContext, OAuth2App>, IOAuth2AppAppService
    {
        private readonly IRepository<OAuth2App> baseOAuth2AppRepository;
        private readonly IOAuth2AppRepository oauth2AppRepository;
        private readonly IOAuth2AccessTokenAppService oauth2AccessTokenAppService;

        public OAuth2AppAppService(
            IRepository<OAuth2App> baseOAuth2AppRepository,
            IOAuth2AppRepository oauth2AppRepository,
            IOAuth2AccessTokenAppService oauth2AccessTokenAppService
            ) : base(baseOAuth2AppRepository)
        {
            this.baseOAuth2AppRepository = baseOAuth2AppRepository;
            this.oauth2AppRepository = oauth2AppRepository;
            this.oauth2AccessTokenAppService = oauth2AccessTokenAppService;
        }

        /// <summary>
        /// 管理 列表
        /// </summary>
        /// <param name="searchArgs">搜索 参数</param>
        /// <param name="pager">分页 参数</param>
        /// <returns></returns>
        public async Task<ListPagerDto<object>> ManageList(SearchArgsDto searchArgs, PagerArgsDto pager)
        {
            var oauth2App = await oauth2AppRepository.ManageList(searchArgs, pager);

            var result = new ListPagerDto<object>();
            result.PagerInfo = oauth2App.PagerInfo;

            var data = new List<object>();
            foreach (var item in oauth2App.Data)
            {
                data.Add(new
                {
                    id = item.Id,
                    createTime = item.CreateTime,
                    createUser = item.CreateUser,
                    updateTime = item.UpdateTime,
                    updateUser = item.UpdateUser,
                    name = item.Name,
                    status = item.Status,
                    remark = item.Remark
                });
            }

            result.Data = data;

            return result;
        }

        /// <summary>
        /// 获取应用 AccessToken
        /// </summary>
        /// <param name="appId">应用 Id</param>
        /// <param name="secret">应用 密钥</param>
        /// <returns></returns>
        public async Task<AppTokenDto> GetToken(int appId, string secret)
        {
            if (appId <= 0)
            {
                throw new UserFriendlyException("请传入有效的 AppId");
            }
            if (string.IsNullOrWhiteSpace(secret))
            {
                throw new UserFriendlyException("请传入有效的 secret");
            }

            var app = await BaseFirstOrDefaultAsync(t => t.Id == appId);
            if (app == null || app.Status != 0)
            {
                throw new UserFriendlyException("应用不存在或已被删除");
            }
            if (!app.Secret.Equals(secret))
            {
                throw new UserFriendlyException("应用Id或密钥有误");
            }

            var token = await oauth2AccessTokenAppService.TokenByAppId(app.Id);
            return token;
        }
    }
}